﻿Imports System.Data.OleDb
Public Class AddPicture
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_picture.FileName)
        If fup_picture.HasFile Then
            fup_picture.SaveAs(Server.MapPath("/Images/UserUploaded") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO Picture (UserName,PictureURL) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("WarriorConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", "/Images/UserUploaded/" & newFileName)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("HomePage.aspx?UserName=" & Request.Params("UserName"))
    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class